var DEBUG = true;

function debug(msg) {
	if(DEBUG) {
		$('#debug').append(msg+'<br />');
	}
}

function setPrevNextButtons(previous, next) {
	if(!previous) {
		$('#action_answer #question #previous').attr('disabled', 'disabled');
		$('#action_answer #question #previous').css('color', '#666666');
	} else { 
		$('#action_answer #question #previous').removeAttr('disabled');
		$('#action_answer #question #previous').css('color', '#FFFFFF');
	}
	
	if(!next) { 
		$('#action_answer #question #next').attr('disabled', 'disabled');
		$('#action_answer #question #next').css('color', '#666666');
	} else { 
		$('#action_answer #question #next').removeAttr('disabled');
		$('#action_answer #question #next').css('color', '#FFFFFF');
	}
}

function showQuestion(content, index, count) {
	if(index <= 0 && index < count - 1) {
		setPrevNextButtons(false, true);
	} else if(index > 0 && index >= count - 1) {
		setPrevNextButtons(true, false);
	} else if(index <= 0 && index == count - 1) {
		setPrevNextButtons(false, false);
	} else {
		setPrevNextButtons(true, true);
	}
	$('#action_answer #question .bubble').text(content);
	$('#action_answer #question #current').text(index+1);
	$('#action_answer #question #all').text(count);
}

$(document).ready(function() {
	var questions = null;
	var current = -1;

	$('#action_answer #response').empty();
	$('.hint').hide();
	$('#action_answer #helper_template').hide();
	
	// $('#action_answer #helper').hide();
	
	//
	//	QUESTION
	//
	$('#action_answer #response').load('getQuestions', function() {
		var strData = $('#action_answer #response').text();
		debug(strData);
		var objData = jQuery.parseJSON(strData);
		if(objData.questions != null) {
			questions = objData.questions;
			if(questions.length > 0) {
				current = 0;
				showQuestion(questions[0].question, 0, questions.length);
			} else {
				showQuestion('Lucky, no questions for you!', -1, 0);
			}
		}else if(objData.redirect !=null)
		{
			window.location=objData.redirect;
		}
		else {
			debug('bad response: questions');
		}
	});
	
	$('#action_answer #question #previous').click(function() {
		debug('previous '+current);
		if(current > 0) {
			current--;
			showQuestion(questions[current].question, current, questions.length);
		}
	});
	
	$('#action_answer #question #next').click(function() {
		debug('next '+current);
		if(current < questions.length - 1) {
			current++;
			showQuestion(questions[current].question, current, questions.length);
		}
	});
	
	//
	//	ANSWER
	//
	$('#action_answer #answer #submit').click(function() {
		$('#action_answer .hint').hide();
		
		if(current < 0 || current >= questions.length) {
			$('#action_answer #answer_warning').text('Warning: undefined question');
			$('#action_answer #answer_warning').show();
		} else {
			var qid = questions[current].id;
			var answer = $('#action_answer #answer_edit').val();
			if(answer.length < 10 || answer > 2048 /* might be 4096 */) {
				$('#action_answer #answer_warning').text('Warning: inappropriate answer');
				$('#action_answer #answer_warning').show();
			} else {
				$('#action_answer #response').load(
					'answerQuestion',
					{ q: qid, a: answer },
					function() {
						$('#action_answer #answer_warning').text('Thank you!');
						$('#action_answer #answer_warning').show();
						questions.splice(current, 1);
						if(questions.length > 0) {
							if(current >= questions.length) {
								current = questions.length - 1;
							}
							showQuestion(questions[current].question, current, questions.length);
						} else {
							current = -1;
							showQuestion('Lucky, no questions for you!', -1, 0);
						}
						$('#action_answer #helper ul').empty();
						$('#action_answer #answer_edit').val('');
						debug('index:'+current);
					}
				);
			}
		}
	});
	
	//
	//	HELPER
	//
	$('#action_answer #answer #help').click(function() {
		$('#action_answer .hint').hide();
		$('#action_answer #response').empty();
		$('#action_answer #helper ul').empty();
		
		if(current >= 0 && current < questions.length) {
		
			var qid = questions[current].id;
			
			$('#action_answer #response').load(
				'helpAnswering',
				{ q: qid },
				function() {
					var strData = $('#action_answer #response').text();
					debug(strData);
					var objData = jQuery.parseJSON(strData);
					if(objData.retrievals != null && objData.retrievals.length > 0) {
						var r = objData.retrievals;
						for (var i in r) {
							$('#action_answer #helper ul').append('<li></li>');
							$('#action_answer #helper_template .one_item').clone().appendTo('#action_answer #helper li:last-child');
							$('#action_answer li:last-child .title').append(r[i].title);
							$('#action_answer li:last-child .url').append(r[i].url);
							$('#action_answer li:last-child .summary').append(r[i].summary);
						}
	
						$('#action_answer .add').click(function() {
							var title = $(this).parents('.one_item').find('.title').text();
							var url = $(this).parents('.one_item').find('.url').text();
							
							var str = 'Refers to (' + title + ', ' + url + ')';
							var val = $('#action_answer #answer_edit').val();
							$('#action_answer #answer_edit').val(val + str);
							debug('add: ' + str);
						});
						
						$('#action_answer .add_all').click(function() {
							var title = $(this).parents('.one_item').find('.title').text();
							var url = $(this).parents('.one_item').find('.url').text();
							var summary = $(this).parents('.one_item').find('.summary').text();
							
							var str = 'Refers to (' + title + ', ' + url + ', ' + summary + ')';
							var val = $('#action_answer #answer_edit').val();
							$('#action_answer #answer_edit').val(val + str);
							debug('add_all: ' + str);
						});
					} else {
						$('#action_answer #answer_warning').text('Hint: Sorry, no hint for this question');
						$('#action_answer #answer_warning').show();
					}
				}
			);
		}
	});
	
	$('#action_answer .add').click(function() {
		debug('add');
	});
});